var championSkins = require("./config").championSkins;

var request = require('request');
var fs = require('fs');

// for (const modelid in championSkins) {
//     if (championSkins.hasOwnProperty(modelid)) {
//         const model = championSkins[modelid];
//         for (const skinid in model) {
//             if (model.hasOwnProperty(skinid)) {
//                 const element = model[skinid];
//                 loadmodel(modelid, skinid);
//             }
//         }
//     }
// }

var loadlist = [];

var modelids = Object.keys(championSkins);
// for (let i = 0; i < modelids.length; i++) {
for (let i = 0; i < 5; i++) {
    const modelid = modelids[i];
    var skinids = Object.keys(championSkins[modelid]);
    // for (let j = 0; j < skinids.length; j++) {
    for (let j = 0; j < 1; j++) {
        const skinid = skinids[j];
        loadlist.push([modelid, skinid]);
    }
}

loadmodels();

function loadmodels() {

    if (loadlist.length == 0) return;
    var loaditem = loadlist.shift();
    var modelid = loaditem[0], skinid = loaditem[1];
    loadmodel(modelid, skinid, () => {
        loadmodels();
    });

    function loadmodel(modelid, skinid, callback) {

        console.log(modelid, skinid);

        loadMesh("mesh", (err, body) => {
            if (err) {
                console.log(err);
                callback()
                return;
            } else {
                var dv = new DataView1(body);
                var version = r.getUint32();
                var animFile = r.getString();
                var textureFile = r.getString();
                console.log("textureFile", textureFile);
            }
            loadMesh("anim", () => {
                callback();
            });
        });

        function loadMesh(type, callback) {
            var meshurl = `http://media.services.zam.com/v1/media/byName/lol/mv/models/${modelid}_${skinid}.l${type}`;
            var savemeshurl = `lol/mv/models/${modelid}_${skinid}.l${type}`;
            var r = request({ url: meshurl, encoding: null }, (error, response, body) => {
                if (error) {
                    callback({ errurl: meshurl });
                    return;
                }
                if (response.statusCode == 200) {
                    r;
                    callback(null, body);
                }
            })
                .pipe(fs.createWriteStream(savemeshurl));
        }
    }
}


function DataView1(buffer) {
    this.buffer = new DataView(buffer);
    this.position = 0
};
DataView1.prototype = {
    getBool: function () {
        var v = this.buffer.getUint8(this.position) != 0;
        this.position += 1; return v
    }, getUint8: function () {
        var v = this.buffer.getUint8(this.position);
        this.position += 1; return v
    }, getInt8: function () {
        var v = this.buffer.getInt8(this.position);
        this.position += 1; return v
    }, getUint16: function () {
        var v = this.buffer.getUint16(this.position, true);
        this.position += 2; return v
    }, getInt16: function () {
        var v = this.buffer.getInt16(this.position, true);
        this.position += 2; return v
    }, getUint32: function () {
        var v = this.buffer.getUint32(this.position, true);
        this.position += 4; return v
    }, getInt32: function () {
        var v = this.buffer.getInt32(this.position, true);
        this.position += 4; return v
    }, getFloat: function () {
        var v = this.buffer.getFloat32(this.position, true);
        this.position += 4; return v
    }, getString: function (len) {
        if (len === undefined) len = this.getUint16(); var str = ""; for (var i = 0; i < len; ++i) {
            str += String.fromCharCode(this.getUint8())
        } return str
    }, setBool: function (v) {
        this.buffer.setUint8(this.position, v ? 1 : 0); this.position += 1
    }, setUint8: function (v) {
        this.buffer.setUint8(this.position, v); this.position += 1
    }, setInt8: function (v) {
        this.buffer.setInt8(this.position, v); this.position += 1
    }, setUint16: function (v) {
        this.buffer.setUint16(this.position, v, true); this.position += 2
    }, setInt16: function (v) {
        this.buffer.setInt16(this.position, v, true); this.position += 2
    }, setUint32: function (v) {
        this.buffer.setUint32(this.position, v, true); this.position += 4
    }, setInt32: function (v) {
        this.buffer.setInt32(this.position, v, true); this.position += 4
    }, setFloat: function (v) {
        this.buffer.setFloat32(this.position, v, true); this.position += 4
    }
};